package com.easy.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.easy.bean.LayData;
import com.easy.bean.Room;
import com.easy.service.Service;
import com.easy.service.impl.RoomService;

@WebServlet("/room/*")
public class RoomServlet extends HttpServlet{
	private Service roomS=new RoomService();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uri=req.getRequestURI();
		//uri:  /hotelms/user/list
		uri=uri.substring(uri.lastIndexOf("/")+1);
		Map params = (Map)req.getAttribute("params");
		
		
		if("list".equals(uri)) {
			List<Room> list=roomS.queryList(params);
			int count=roomS.queryCount(params);
			//封装成LayData对象
			//因为LayUI需要固定的格式来解析数据
			LayData lay=new LayData(count,list);
			//将lay对象转化成json数据,将json数据返回给前台
			String json=JSON.toJSONString(lay);
			resp.getWriter().write(json);
			return;
		}else if("add".equals(uri)) {

			if("on".equals(params.get("r_state"))) {
				params.put("r_state", "空闲");
			}
			else if(params.get("r_state")==null) {
				params.put("r_state", "入住");
			}
			int count = roomS.insert(params);
			resp.getWriter().write(count);
			return;
		}else if("del".equals(uri)) {
			
			
			
			int count = roomS.delete(params);
			resp.getWriter().write(count);
			return;
		}else if("upd".equals(uri)) {
			
			
			if("on".equals(params.get("r_state"))) {
				params.put("r_state", "空闲");
			}
			else if(params.get("r_state")==null) {
				params.put("r_state", "入住");
			}
			int count = roomS.update(params);
			resp.getWriter().write(count);
			return;
		}
	}
}
